package org.ainlolcat.ainscience.statistics.handlers.fitting;

import org.ainlolcat.ainscience.utils.ArraysUtil;
import org.ainlolcat.ainscience.statistics.handlers.fitting.functions.GaussFunction;
import org.ainlolcat.ainscience.statistics.handlers.fitting.functions.PsdVoigtFunction;
import org.apache.commons.math.optimization.fitting.CurveFitter;
import org.apache.commons.math.optimization.fitting.ParametricRealFunction;
import org.apache.commons.math.optimization.general.LevenbergMarquardtOptimizer;

/**
* @author ainlolcat
*         Date: 5/9/13
*/
public class PsvVoigtPeakFitting  extends AbstractPeakFitting{

    public PsvVoigtPeakFitting(PeakFittingStatisticHandler peakFittingStatisticHandler) {
        super(peakFittingStatisticHandler);
        function = new PsdVoigtFunction();
    }

    protected double[] prepareGraph(double[] x, double[] y){
        maximum = y[0];
        mean = x[0];
        double ySumm = 0;
        for (int i=0;i<(x.length<y.length?x.length:y.length);i++){
            maximum = y[i]>maximum?y[i]:maximum;
            ySumm+=y[i];
            mean += y[i]*x[i];
            disp += y[i]*x[i]*x[i];
        }
        mean = mean / ySumm;
        disp = disp / ySumm - mean * mean;
        double[] y1 = new double[x.length<y.length?x.length:y.length];
        for (int i=0;i<(x.length<y.length?x.length:y.length);i++){
            y1[i] = y[i]/maximum;
        }
        return y1;
    }
}
